package com.qst.dao.impl;

import com.qst.dao.UserDao;
import com.qst.entity.Role;
import com.qst.entity.User;
import com.qst.utils.DBHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserMySqlDaoImpl implements UserDao {

    @Override
    public List<User> getAll() {
        List<User> userList =new ArrayList<>();
        DBHelper dbHelper=new DBHelper();
        dbHelper.setSql("Select * from user");
        ResultSet resultSet = dbHelper.executeQuery();

        try {
            while (resultSet.next()) {
                User user=new User(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3)
                );
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public User getUserByNameAndPwd(String userName, String userPwd) {
        User result=null;
        DBHelper dbHelper=new DBHelper();
        dbHelper.setSql("Select * from user where userName=? and userPwd=?");
        List<String> list = new ArrayList<>();
        list.add(userName);
        list.add(userPwd);
        dbHelper.setSqlValues(list);
        ResultSet resultSet = dbHelper.executeQuery();
        try {
            if (resultSet.next()) {
                result=new User(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3)
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
